import toLocalTime from './toLocalTime'
const toRelativeTime = (isoString: string) => {
    const now = new Date().getTime();
    const pastDate = new Date(isoString).getTime();
    const secondsPast = Math.floor((now - pastDate) / 1000); // 转换为秒

    // 检查是否在过去7天内
    if (secondsPast < 60 * 60 * 24 * 7) {
        // 刚刚
        if (secondsPast < 60) {
            return '刚刚';
        }
        // xx分钟前
        else if (secondsPast < 60 * 60) {
            const minutes = Math.floor(secondsPast / 60);
            return `${minutes}分钟前`;
        }
        // xx小时前
        else if (secondsPast < 60 * 60 * 24) {
            const hours = Math.floor(secondsPast / (60 * 60));
            return `${hours}小时前`;
        } else {
            const days = Math.floor(secondsPast / (60 * 60 * 24));
            return `${days}天前`;
        }
    } else {
        // 超过7天，显示具体日期和时间
        return toLocalTime(isoString)
    }
}

export default toRelativeTime